package com.user_hosted_storage.server.dal;

import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.Query;

import com.user_hosted_storage.entities.Client;

/**
 * Session Bean implementation class HostFacade
 */
@Stateless
@LocalBean
public class ClientFacade extends GenericFacade<Client> {

    /**
     * Default constructor. 
     */
    public ClientFacade() {
    	
    }
    
    public Client getByUserNameAndPassword(String userName, String passwordHash) {
		Query query = em.createQuery("SELECT c FROM "+getEntityName()+" c WHERE c.userName = :userName and c.passwordHash = :passwordHash");
		
		query.setParameter("userName", userName);
		query.setParameter("passwordHash", passwordHash);
		
		@SuppressWarnings("unchecked")
		List<Client> resultList = query.getResultList();
		
		if(resultList.size() == 0) {
			return null;
		}
		
		return resultList.get(0);
	}
	
	public Client getByUserNameOrEmail(String userName, String email) {
		Query query = em.createQuery("SELECT c FROM "+getEntityName()+" c WHERE c.userName = :userName OR c.email = :email");
		
		query.setParameter("userName", userName);
		query.setParameter("email", email);
		
		@SuppressWarnings("unchecked")
		List<Client> resultList = query.getResultList();
		
		if(resultList.size() == 0) {
			return null;
		}
		
		return resultList.get(0);
	}
	
	public Client getByUserName(String userName) {
		Query query = em.createQuery("SELECT c FROM "+getEntityName()+" c WHERE c.userName = :userName");
		
		query.setParameter("userName", userName);
		
		@SuppressWarnings("unchecked")
		List<Client> resultList = query.getResultList();
		
		if(resultList.size() == 0) {
			return null;
		}
		
		return resultList.get(0);
	}

	@SuppressWarnings("unchecked")
	public List<Client> getUnregisteredJabber() {
		Query query = em.createQuery("select c from Client c where c.registeredJabber = false");
		return query.getResultList();
	}	
}
